import { Byte } from '../../api/io/Byte';
import { ITimer } from '../../api/timer/ITimer';
import { IAsync } from '../../api/event/IAsync';



/**属性枚举*/
export const enum E_ApplyInfo {
	/***/
	PLAYER_ID = 0,
	/** 申请时间*/
	APPLY_TIME = 1,
	/**昵称*/
	NICKNAME = 2,
	/**职业*/
	PROFESSION = 3,
	/**等级*/
	LEVEL = 4,
	/**头像索引*/
	HEAD_ICON = 5,
	/**战力*/
	FIGHT_SCORE = 6,
	/**枚举数量*/
	EnumCount = 7
}


/***/
export class ApplyInfo {
	/**事件ID*/
	public static readonly EVENT_ID = 'STRUCT_DATA_APPLY_INFO_EVENT_ID';
	/**整体更新事件ID（部分只需要监听整体变化）*/
	public static readonly TOTAL_EVENT_ID = 'STRUCT_DATA_APPLY_INFO_TOTAL_EVENT_ID';
	/**缓存池*/
	private static POOL: ApplyInfo[] = [];

	/**创建入口*/
	public static create(json?: string): ApplyInfo {
		const c = this.POOL.pop() || new ApplyInfo();
		c._destroyed = false;
		c.resetProperty();
		if (json) {
			c.parse(json);
		}
		return c;
	}


	/**已释放*/
	protected _destroyed = false;
	/** (uint32)*/
	protected _playerId: number;
	/** 申请时间 (long)*/
	protected _applyTime: number;
	/**昵称*/
	protected _nickname: string;
	/**职业 (uint8)*/
	protected _profession: number;
	/**等级 (uint32)*/
	protected _level: number;
	/**头像索引 (uint8)*/
	protected _headIcon: number;
	/**战力 (long)*/
	protected _fightScore: number;


	/**构建函数-私有，防止外部创建*/
	private constructor() {
	}


	/**释放*/
	public destroy(): void {
		if (this._destroyed) {
			return;
		}
		this._destroyed = true;
		this.resetProperty(true);
		if (ApplyInfo.POOL.indexOf(this) === -1) {
			ApplyInfo.POOL.push(this);
		}
	}

	/***/
	public get playerId() {
		return this._playerId;
	}


	/***/
	public set playerId(v: number) {
		this._playerId = v;
	}


	/** 申请时间*/
	public get applyTime() {
		return this._applyTime;
	}


	/** 申请时间*/
	public set applyTime(v: number) {
		this._applyTime = v;
	}


	/**昵称*/
	public get nickname() {
		return this._nickname;
	}


	/**昵称*/
	public set nickname(v: string) {
		this._nickname = v;
	}


	/**职业*/
	public get profession() {
		return this._profession;
	}


	/**职业*/
	public set profession(v: number) {
		this._profession = v;
	}


	/**等级*/
	public get level() {
		return this._level;
	}


	/**等级*/
	public set level(v: number) {
		this._level = v;
	}


	/**头像索引*/
	public get headIcon() {
		return this._headIcon;
	}


	/**头像索引*/
	public set headIcon(v: number) {
		this._headIcon = v;
	}


	/**战力*/
	public get fightScore() {
		return this._fightScore;
	}


	/**战力*/
	public set fightScore(v: number) {
		this._fightScore = v;
	}



	/**通过枚举获取值*/
	public getV(k: E_ApplyInfo): any {
		switch (k) {
			case E_ApplyInfo.PLAYER_ID: return this._playerId;
			case E_ApplyInfo.APPLY_TIME: return this._applyTime;
			case E_ApplyInfo.NICKNAME: return this._nickname;
			case E_ApplyInfo.PROFESSION: return this._profession;
			case E_ApplyInfo.LEVEL: return this._level;
			case E_ApplyInfo.HEAD_ICON: return this._headIcon;
			case E_ApplyInfo.FIGHT_SCORE: return this._fightScore;
			default: return null;
		}
	}


	/**通过枚举设置值*/
	public setV(k: E_ApplyInfo, v: any): void {
		switch (k) {
			case E_ApplyInfo.PLAYER_ID: this._playerId = v; break;
			case E_ApplyInfo.APPLY_TIME: this._applyTime = v; break;
			case E_ApplyInfo.NICKNAME: this._nickname = v; break;
			case E_ApplyInfo.PROFESSION: this._profession = v; break;
			case E_ApplyInfo.LEVEL: this._level = v; break;
			case E_ApplyInfo.HEAD_ICON: this._headIcon = v; break;
			case E_ApplyInfo.FIGHT_SCORE: this._fightScore = v; break;
			default: break;
		}
	}


	/**初始化*/
	public resetProperty(fromDestroy = false): void {
		//
		this._playerId = 0;
		// 申请时间
		this._applyTime = 0;
		//昵称
		this._nickname = '';
		//职业
		this._profession = 0;
		//等级
		this._level = 0;
		//头像索引
		this._headIcon = 0;
		//战力
		this._fightScore = 0;
	}


	/**序列化*/
	public stringify(toString: boolean): string | any {
		const json: any = {};
		if (this._playerId) {
			json.playerId = this._playerId;
		}
		if (this._applyTime) {
			json.applyTime = this._applyTime;
		}
		if (this._nickname) {
			json.nickname = this._nickname;
		}
		if (this._profession) {
			json.profession = this._profession;
		}
		if (this._level) {
			json.level = this._level;
		}
		if (this._headIcon) {
			json.headIcon = this._headIcon;
		}
		if (this._fightScore) {
			json.fightScore = this._fightScore;
		}

		return toString ? JSON.stringify(json) : json;
	}


	/**反序列化*/
	public parse(jsonv: string | any): this {
		this.resetProperty();
		if (!jsonv) {
			return this;
		}
		const json = typeof (jsonv) === 'string' ? JSON.parse(jsonv) : jsonv;
		if (json.playerId) {
			this._playerId = json.playerId;
		}
		if (json.applyTime) {
			this._applyTime = json.applyTime;
		}
		if (json.nickname) {
			this._nickname = json.nickname;
		}
		if (json.profession) {
			this._profession = json.profession;
		}
		if (json.level) {
			this._level = json.level;
		}
		if (json.headIcon) {
			this._headIcon = json.headIcon;
		}
		if (json.fightScore) {
			this._fightScore = json.fightScore;
		}
		return this;
	}


	/**序列化-二进制*/
	public toBytes(byte: Byte): void {
		if (!byte) {
			return;
		}
		byte.writeUint32(this._playerId);
		byte.writeLongNumber(this._applyTime);
		byte.writeString(this._nickname);
		byte.writeUint8(this._profession);
		byte.writeUint32(this._level);
		byte.writeUint8(this._headIcon);
		byte.writeLongNumber(this._fightScore);
	}


	/**反序列化-二进制*/
	public fromBytes(byte: Byte): this {
		this.resetProperty();
		if (!byte) {
			return;
		}
		this._playerId = byte.readUint32();
		this._applyTime = byte.readLongNumber();
		this._nickname = byte.readString();
		this._profession = byte.readUint8();
		this._level = byte.readUint32();
		this._headIcon = byte.readUint8();
		this._fightScore = byte.readLongNumber();
		return this;
	}


	/**克隆*/
	public clone(): ApplyInfo {
		const byte = Byte.createFromPool();
		this.toBytes(byte);
		byte.pos = 0;
		const clonev = ApplyInfo.create().fromBytes(byte);
		Byte.recoverToPool(byte);
		return clonev;
	}


	/**处理更新*/
	public processUpdate(jsonv: string): void {
		if (!jsonv) {
			return;
		}

		const eventID = ApplyInfo.EVENT_ID;
		const us = JSON.parse(jsonv);

		for (let i = 0, len = us.length; i < len;) {
			const idx = us[i];
			//
			if (idx === E_ApplyInfo.PLAYER_ID) {
				const oldv = this._playerId;
				this._playerId = us[i + 1];
				IAsync.dispatch(eventID, idx, this._playerId, oldv);
				i += 2;
				continue;
			}
			// 申请时间
			if (idx === E_ApplyInfo.APPLY_TIME) {
				const oldv = this._applyTime;
				this._applyTime = us[i + 1];
				IAsync.dispatch(eventID, idx, this._applyTime, oldv);
				i += 2;
				continue;
			}
			//昵称
			if (idx === E_ApplyInfo.NICKNAME) {
				const oldv = this._nickname;
				this._nickname = us[i + 1];
				IAsync.dispatch(eventID, idx, this._nickname, oldv);
				i += 2;
				continue;
			}
			//职业
			if (idx === E_ApplyInfo.PROFESSION) {
				const oldv = this._profession;
				this._profession = us[i + 1];
				IAsync.dispatch(eventID, idx, this._profession, oldv);
				i += 2;
				continue;
			}
			//等级
			if (idx === E_ApplyInfo.LEVEL) {
				const oldv = this._level;
				this._level = us[i + 1];
				IAsync.dispatch(eventID, idx, this._level, oldv);
				i += 2;
				continue;
			}
			//头像索引
			if (idx === E_ApplyInfo.HEAD_ICON) {
				const oldv = this._headIcon;
				this._headIcon = us[i + 1];
				IAsync.dispatch(eventID, idx, this._headIcon, oldv);
				i += 2;
				continue;
			}
			//战力
			if (idx === E_ApplyInfo.FIGHT_SCORE) {
				const oldv = this._fightScore;
				this._fightScore = us[i + 1];
				IAsync.dispatch(eventID, idx, this._fightScore, oldv);
				i += 2;
				continue;
			}

			console.error('[Struct Data] can not find enum in ApplyInfo with idx = ' + idx + ', version maybe wrong.')
		}

		//全更事件
		ITimer.callLater(this, this.dispatchTotalUpdate);
	}


	/**比对不一致结果*/
	public getCompareInfo(other: ApplyInfo): string {
		if (!other) {
			return null;
		}

		const updates: any[] = [];

		if (this._playerId !== other._playerId) {
			updates.push(E_ApplyInfo.PLAYER_ID, this._playerId);
		}
		if (this._applyTime !== other._applyTime) {
			updates.push(E_ApplyInfo.APPLY_TIME, this._applyTime);
		}
		if (this._nickname !== other._nickname) {
			updates.push(E_ApplyInfo.NICKNAME, this._nickname);
		}
		if (this._profession !== other._profession) {
			updates.push(E_ApplyInfo.PROFESSION, this._profession);
		}
		if (this._level !== other._level) {
			updates.push(E_ApplyInfo.LEVEL, this._level);
		}
		if (this._headIcon !== other._headIcon) {
			updates.push(E_ApplyInfo.HEAD_ICON, this._headIcon);
		}
		if (this._fightScore !== other._fightScore) {
			updates.push(E_ApplyInfo.FIGHT_SCORE, this._fightScore);
		}

		const jsonv = updates.length === 0 ? null : JSON.stringify(updates);
		return jsonv;
	}


	/**全部更新*/
	protected dispatchTotalUpdate(): void {
		IAsync.dispatch(ApplyInfo.TOTAL_EVENT_ID, this.getV(0));
	}

}
